package sweetguy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import sweetguy.dto.PageDTO;
import sweetguy.dto.RoomInsertDTO;
import sweetguy.dto.RoomUpdateDTO;
import sweetguy.entity.dto.Room;
import sweetguy.exception.ServiceException;
import sweetguy.mapper.RoomMapper;
import sweetguy.service.RoomService;

import java.time.LocalDateTime;
import java.util.List;

@Service
@CacheConfig(cacheNames = "room")
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomMapper roomMapper;

    @Override
    @CacheEvict(allEntries = true)
    public int insert(RoomInsertDTO roomInsertDTO) {
        Room room = BeanUtil.copyProperties(roomInsertDTO, Room.class);
        // 设置默认值
        room.setId(null);
        room.setDeleted(0);
        room.setCreated(LocalDateTime.now());
        room.setUpdated(LocalDateTime.now());
        // DB添加
        int result = roomMapper.insert(room);
        if (result <= 0) {
            throw new RuntimeException("DB添加失败");
        }
        return result;
    }

    @Cacheable(key = "#id", condition = "#id != null", unless = "#result == null ")
    @Override
    public Room selectById(Long id) {
        Room result = roomMapper.selectById(id);
        if (ObjectUtil.isNull(result)) {
            throw new ServiceException("ID不存在");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int updateById(RoomUpdateDTO roomUpdateDTO) {
        Room room = BeanUtil.copyProperties(roomUpdateDTO, Room.class);
        // 设置默认值
        room.setCreated(null);
        room.setUpdated(LocalDateTime.now());
        int result = roomMapper.updateById(room);
        // DB修改
        if (result <= 0) {
            throw new ServiceException("DB修改失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int logicDeleteById(Long id) {
        //todo 查询该记录是否被引用
        int result = roomMapper.logicDeleteById(id);
        if (result <= 0) {
            throw new ServiceException("DB删除失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int logicDeleteByIds(List<Long> ids) {
        //todo
        int result = roomMapper.logicDeleteByIds(ids);
            if (result <= 0) {
                throw new ServiceException("DB逻辑批量删除失败");
        }
        return result;
    }

    @Cacheable(key = "#root.methodName", unless = "#result == null ")
    @Override
    public List<Room> list() {
        return roomMapper.list();
    }

    @Cacheable(key = "#root.methodName + ':' + " +
               "#pageDTO.getPageNum() + ':' + " +
               "#pageDTO.getPageSize()", unless = "#result == null ")
    @Override
    public PageInfo<Room> page(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
        return new PageInfo<>(roomMapper.list());
    }
}
